package org.base23.video.domain.convertor;

import org.base23.video.domain.dto.AiGuidanceConversation;
import org.base23.video.domain.dto.AiGuidanceMessage;
import org.base23.video.domain.entity.AiGuidanceConversationEntity;
import org.base23.video.domain.entity.AiGuidanceMessageEntity;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * AI指导功能转换器
 */
public class AiGuidanceConvertor {

    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * Entity转DTO - 对话
     */
    public static AiGuidanceConversation toDto(AiGuidanceConversationEntity entity) {
        if (entity == null) return null;

        AiGuidanceConversation dto = new AiGuidanceConversation();
        dto.setConversationId(entity.getId());
        dto.setSportType(entity.getSportType());
        dto.setUserId(entity.getUserId());
        
        // 转换状态
        if (entity.getStatus() != null) {
            dto.setStatus(AiGuidanceConversation.ConversationStatus.valueOf(entity.getStatus()));
        }
        
        // 转换时间
        if (entity.getCreateTime() != null) {
            dto.setCreateTime(entity.getCreateTime());
        }
        if (entity.getLastUpdateTime() != null) {
            dto.setLastUpdateTime(entity.getLastUpdateTime());
        }

        return dto;
    }

    /**
     * DTO转Entity - 对话
     */
    public static AiGuidanceConversationEntity toEntity(AiGuidanceConversation dto) {
        if (dto == null) return null;

        AiGuidanceConversationEntity entity = new AiGuidanceConversationEntity();
        entity.setId(dto.getConversationId());
        entity.setSportType(dto.getSportType());
        entity.setUserId(dto.getUserId());
        
        // 转换状态
        if (dto.getStatus() != null) {
            entity.setStatus(dto.getStatus().name());
        }
        
        // 转换时间
        if (dto.getCreateTime() != null) {
            entity.setCreateTime(dto.getCreateTimeAsLocalDateTime());
        }
        if (dto.getLastUpdateTime() != null) {
            entity.setLastUpdateTime(dto.getLastUpdateTimeAsLocalDateTime());
        }

        return entity;
    }

    /**
     * Entity转DTO - 消息
     */
    public static AiGuidanceMessage toDto(AiGuidanceMessageEntity entity) {
        if (entity == null) return null;

        AiGuidanceMessage dto = new AiGuidanceMessage();
        dto.setMessageId(entity.getId());
        dto.setContent(entity.getContent());
        
        // 转换发送者
        if (entity.getSender() != null) {
            if ("USER".equals(entity.getSender())) {
                dto.setSenderType(AiGuidanceMessage.SenderType.USER);
            } else if ("AI".equals(entity.getSender())) {
                dto.setSenderType(AiGuidanceMessage.SenderType.AI_ASSISTANT);
            }
        }
        
        // 转换时间
        if (entity.getTimestamp() != null) {
            dto.setTimestamp(entity.getTimestamp());
        }

        return dto;
    }

    /**
     * DTO转Entity - 消息
     */
    public static AiGuidanceMessageEntity toEntity(AiGuidanceMessage dto, String conversationId) {
        if (dto == null) return null;

        AiGuidanceMessageEntity entity = new AiGuidanceMessageEntity();
        entity.setId(dto.getMessageId());
        entity.setConversationId(conversationId);
        entity.setContent(dto.getContent());
        entity.setMessageType("TEXT"); // 默认文本类型
        
        // 转换发送者
        if (dto.getSenderType() != null) {
            entity.setSender(dto.getSenderType().name());
        }
        
        // 转换时间
        if (dto.getTimestamp() != null) {
            entity.setTimestamp(dto.getTimestampAsLocalDateTime());
        }

        return entity;
    }

    /**
     * Entity列表转DTO列表 - 对话
     */
    public static List<AiGuidanceConversation> toDtoList(List<AiGuidanceConversationEntity> entities) {
        if (entities == null) return null;
        return entities.stream().map(AiGuidanceConvertor::toDto).collect(Collectors.toList());
    }

    /**
     * Entity列表转DTO列表 - 消息
     */
    public static List<AiGuidanceMessage> toMessageDtoList(List<AiGuidanceMessageEntity> entities) {
        if (entities == null) return null;
        return entities.stream().map(AiGuidanceConvertor::toDto).collect(Collectors.toList());
    }

    /**
     * 从对话DTO和消息Entity列表创建完整的对话DTO
     */
    public static AiGuidanceConversation toConversationWithMessages(AiGuidanceConversationEntity conversationEntity, 
                                                                   List<AiGuidanceMessageEntity> messageEntities) {
        AiGuidanceConversation conversation = toDto(conversationEntity);
        if (conversation != null && messageEntities != null) {
            conversation.setMessages(toMessageDtoList(messageEntities));
        }
        return conversation;
    }
} 